import http from '../../../utils/https.js';
Page({
  data: {
    parkingList: [],
    hasParkingSpace: true
  },
  onLoad: function (options) {
    const residentId = wx.getStorageSync('residentId');
    console.log('当前的residentId', residentId);
    if (residentId) {
      this.getParkingSpaceList(residentId);
    } else {
      wx.showToast({
        title: '请先登录',
        icon: 'none'
      })
    }

  },
  getParkingSpaceList(residentId) {
    const id = parseInt(residentId);
    http.get('parkingSpace/parkingSpace/list', {
        residentId: id
      })
      .then(res => {
        console.log('获取到的数据', res)
        if (!res.rows || res.rows.length === 0) {
          // 没有车位信息
          this.setData({
            parkingList: [],
            hasParkingSpace: false
          });
          return;
        }
        
        const list=res.rows.map(item=>{
          let statusText='';
          let statusClass='';
          switch(item.auditStatus){
            case 0:
              statusText='审核成功';
              statusClass='status-success';
              break;
            case 1:
              statusText='审核中';
              statusClass='status-pending';
              break;
            case 2:
              statusText='审核未通过';
              statusClass='status-failed';
              break;
            default:
              statusText='未知状态';
              statusClass='';
          }
          return {
            ...item,
            statusText:statusText,
            statusClass:statusClass,
          };
        });
        this.setData({
          parkingList: list,
          hasParkingSpace: true
        })
      }).catch(err => {
        console.log('请求失败', err);
        this.setData({
          parkingList: [],
          hasParkingSpace: false
        });
        wx.showToast({
          title: '获取车位信息失败',
          icon: 'none'
        });
      });
  },
  gotoCarSpaceDetails(e) {
    const index = e.currentTarget.dataset.index;
    const item = this.data.parkingList[index];
    
    if (!item || !item.id) {
      wx.showToast({
        title: '车位信息不完整',
        icon: 'none'
      });
      return;
    }
    
    console.log('跳转到车位详情，车位ID:', item.id, '车位信息:', item);
    
    wx.navigateTo({
      url: `/mine/pages/carspace-details/carspace-details?id=${item.id}&status=${item.auditStatus}`,
      success: () => {
        console.log('成功跳转到车位详情页面');
      },
      fail: (err) => {
        console.error('跳转车位详情页面失败:', err);
        wx.showToast({
          title: '页面跳转失败',
          icon: 'none'
        });
      }
    });
  },

});